/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.serializer.changeser.entity.deserializer;

import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.item.ClassGeneratorContext;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.coreGenerator.base.serializer.changeser.ChangeSerGenContext;
import com.inspur.edp.cef.generator.coreGenerator.base.serializer.changeser.dataType.deserializer.ChangeDeserClassGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.serializer.changeser.entity.serializer.IsChildObjectCodeGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.serializer.changeser.entity.serializer.NodeDataSerGetTypesGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.serializer.dataser.serializer.EntityDataSerGenContext;
import com.inspur.edp.cef.spi.jsonser.entity.AbstractEntityChangeJsonDeSerializer;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import org.eclipse.jdt.core.dom.Modifier;

import java.util.ArrayList;
import java.util.Arrays;

public class NodeDataChangeDeserClassGenerator extends ChangeDeserClassGenerator {

    protected final String nodeCode;
    protected final boolean isRoot;
    protected final NodeDataChangeDeserClassGenCtx parentSerGenContext;
    protected IGspCommonObject object;

    public NodeDataChangeDeserClassGenerator(
            IGspCommonObject object, ChangeSerGenContext edChangeSerGenContext, NodeDataChangeDeserClassGenCtx parentSerGenContext) {
        super(object, edChangeSerGenContext);
        this.object = object;
        this.nodeCode = object.getCode();
        this.isRoot = object.getParentObject() == null;
        this.parentSerGenContext = parentSerGenContext;
    }

    @Override
    protected void beforeInitialize() {
        super.beforeInitialize();
        if (isRoot) {
            changeSerGenContext.setDeserializerClassName(getName());
        } else {
            parentSerGenContext.getChildSerTypes().put(nodeCode, new MediateType("", getName()));
        }
    }

    @Override
    protected ClassGeneratorContext createClassInfoContext() {
        return new NodeDataChangeDeserClassGenCtx(nodeCode, isRoot, changeSerGenContext);
    }

    @Override
    protected java.util.ArrayList<ClassMethodGenerator> createMethodGenerators() {
        NodeDataChangeDeserClassGenCtx nodeDataChangeDeserClassGenCtx = getNodeDataChangeDeserGenContext();
        return new ArrayList<ClassMethodGenerator>(Arrays.asList(new ClassMethodGenerator[]
                {
                        new NodeDataSerGetTypesGenerator(object, nodeDataChangeDeserClassGenCtx.getEDSerGenContext().getMgrContext()),
                        new NodeDataChangeGetChildGenerator(nodeDataChangeDeserClassGenCtx)
                        , new IsChildObjectCodeGenerator(object)
                        , new GetDataConvertorGenerator(
                        (EntityDataSerGenContext) nodeDataChangeDeserClassGenCtx.getEDSerGenContext().getMgrContext().getDataSerContext()
                        , getNodeDataChangeDeserGenContext().getIsRoot()
                        , getNodeDataChangeDeserGenContext().getNodeCode())}));
    }

    protected NodeDataChangeDeserClassGenCtx getNodeDataChangeDeserGenContext() {
        return (NodeDataChangeDeserClassGenCtx) super.getContext();
    }

    @Override
    protected ArrayList<Modifier.ModifierKeyword> getAccessModifier() {
        ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
        list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
        return list;
    }

    @Override
    protected java.util.ArrayList<BaseGenerator> createClassExtendChildGenrators() {
        java.util.ArrayList<BaseGenerator> list = new java.util.ArrayList<BaseGenerator>();

        for (IGspCommonObject item : object.getContainChildObjects()) {
            list.add(getEntityChangeSerClassGenerator(item));
        }
        return list;
    }

    public NodeDataChangeDeserClassGenerator getEntityChangeSerClassGenerator(IGspCommonObject item) {
        return new NodeDataChangeDeserClassGenerator(item, super.changeSerGenContext, getNodeDataChangeDeserGenContext());
        //( serializerTypes, nodeCode, isRoot, entityTypes, ChangeSerGenContext, getNodeDataChangeSerGenContext());
    }

    @Override
    protected String getName() {
        return nodeCode + "ChangeDeserConvertor";
    }

    @Override
    protected TypeInfo setParentClass() {
        return new TypeInfo(AbstractEntityChangeJsonDeSerializer.class);
    }

}
